package com.xianbeiying.app.ckgl.http;

import android.content.Context;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.BinaryHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import cz.msebera.android.httpclient.entity.StringEntity;
import cz.msebera.android.httpclient.message.BasicHeader;
import cz.msebera.android.httpclient.protocol.HTTP;

/**
 * Created by Administrator on 2016/5/26.
 */
public class HttpPostManger{
    private  static AsyncHttpClient client=new AsyncHttpClient(); //实例化对象

    static
    {
        client.setTimeout(25000);   //设置链接超时，如果不设置，默认为10s
//        client.addHeader(HTTP.CONTENT_TYPE, RequestParams.APPLICATION_JSON);
    }

    public static void get(String urlString,AsyncHttpResponseHandler res)    //用一个完整url获取一个string对象
    {
        client.get(urlString, res);
    }
    public static void get(String urlString,RequestParams params,AsyncHttpResponseHandler res)   //url里面带参数
    {
        client.get(urlString, params,res);
    }
    public static void get(String urlString,JsonHttpResponseHandler res)   //不带参数，获取json对象或者数组
    {
        client.get(urlString, res);
    }
    public static void get(String urlString,TextHttpResponseHandler res)   //不带参数，获取json对象或者数组
    {
        client.get(urlString, res);
    }
    public static void get(String urlString,RequestParams params,JsonHttpResponseHandler res)   //带参数，获取json对象或者数组
    {
        client.get(urlString, params,res);
    }
    public static void get(String uString, BinaryHttpResponseHandler bHandler)   //下载数据使用，会返回byte数据
    {
        client.get(uString, bHandler);
    }
    public static AsyncHttpClient getClient()
    {
        return client;
    }

    public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        try {
            client.post(url, params, responseHandler);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void post(Context context,String url, Object object, AsyncHttpResponseHandler responseHandler) {
        try {
            StringEntity se = new StringEntity(object.toString(),"utf-8");
            se.setContentType(RequestParams.APPLICATION_JSON);
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, RequestParams.APPLICATION_JSON));
            client.post(context,url, se,RequestParams.APPLICATION_JSON, responseHandler);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//    public static String ConvertStreamToString(InputStream is) {
//
//        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
//
//        StringBuilder sb = new StringBuilder();
//
//        String line = null;
//
//        try {
//
//            while ((line = reader.readLine()) != null) {
//
//                sb.append(line + "\n");
//
//            }
//
//        } catch (IOException e) {
//
//            System.out.println("Error=" + e.toString());
//
//        } finally {
//
//            try {
//
//                is.close();
//
//            } catch (IOException e) {
//
//                System.out.println("Error=" + e.toString());
//
//            }
//
//        }
//
//        return sb.toString();
//
//    }


    public static void post(Context context,String url, Object object, JsonHttpResponseHandler responseHandler) {
        try {
            StringEntity se = new StringEntity(object.toString(),"utf-8");
            se.setContentType(RequestParams.APPLICATION_JSON);
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, RequestParams.APPLICATION_JSON));
            client.post(context,url, se,RequestParams.APPLICATION_JSON, responseHandler);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void post(Context context,String url, Object object, TextHttpResponseHandler responseHandler) {
        try {
            StringEntity se = new StringEntity(object.toString(),"utf-8");
            se.setContentType(RequestParams.APPLICATION_JSON);
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, RequestParams.APPLICATION_JSON));
            client.post(context,url, se,RequestParams.APPLICATION_JSON, responseHandler);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
